package com.kyzh.core.pager.weal.huodong

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.gushenge.core.beans.WealDoing
import com.gushenge.core.dao.GlobalConsts
import com.gushenge.core.dao.GlobalKeys
import com.gushenge.core.requests.UserRequest
import com.kyzh.core.R
import com.kyzh.core.activities.BrowserActivity
import com.kyzh.core.databinding.FragGrowthBinding
import com.gushenge.core.base.fragment.BaseFragment
import com.kyzh.core.utils.loadImage
import com.makeramen.roundedimageview.RoundedImageView
import org.jetbrains.anko.support.v4.startActivity

class HuoDongFragment : BaseFragment() {
    private lateinit var db : FragGrowthBinding
    private val adapter = Adapter()
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        db  = FragGrowthBinding.inflate(inflater)
        return db.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        db.rev.adapter = adapter
        initData()
    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        if(!hidden){
            initData()
        }
    }

    override fun onResume() {
        super.onResume()
        initData()
    }
    fun initData(){
        UserRequest.wealHuodong {
            adapter.setNewInstance(this)
        }
    }
    private inner class Adapter :BaseQuickAdapter<WealDoing, BaseViewHolder>(R.layout.item_weal3){
        override fun convert(holder: BaseViewHolder, item: WealDoing) {
            holder.setText(R.id.tvTitle,item.title)
                .setText(R.id.tvTime,"${getString(R.string.addedTime)}: ${item.time}")
            holder.getView<RoundedImageView>(R.id.ivImage).loadImage(requireActivity(),item.image)
            holder.itemView.setOnClickListener {
                startActivity<BrowserActivity>(GlobalKeys.TITLE to item.title, GlobalKeys.LINK to GlobalConsts.NEWS_XQ + item.id)
            }
        }

    }
}